package org.esgi.ppa.HUI;


import java.awt.Dimension;
import java.awt.HeadlessException;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class UnitTestingWindow extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private MainWindow mw;
	JTextField tfUrl;
	JTextField tfNbRequests;
	JTextField tfNbConnexions;
	JTextField tfPath;
	JTextField tfHeaderName, tfParameterName;
	JTextField tfHeaderValue, tfParameterValue;
	JButton bResult;	
	public JButton bAccept;
	JButton bCancel;
	JButton bAddHeader;
	JButton bAddParameter;
	ControlButtonForTestWindow cb;
	JComboBox cbMethod;
	JLabel antiBug;
	JLabel lUrl;
	JLabel lMethod;
	JLabel lHeaders;
	JLabel lParameters;
	JLabel lResult;
	JLabel lNbRequest;
	JLabel lNbConnexion;
	ImageIcon iiOK, iiAdd, iiCancel;
	JTable tbForm;
	JPanel parametersPan, headersPan, addedHeadersPan, addedParametersPan;
	
	
	public UnitTestingWindow(MainWindow mw){
		this.mw = mw;
		
		initAttribute();
		makeWindow();
		
		setTitle("Test unitaire n� " + 1);
        setDefaultLookAndFeelDecorated(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        pack();
        Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        int hauteur = (int)screenSize.getHeight();
        int largeur = (int)screenSize.getWidth();

        setBounds((largeur/2)-350, (hauteur/2)-300, 700, 600);
        setVisible(true);        
	}
	
	private void initAttribute(){		
		Object[] optionsForMethod = {"POST", "GET"};

		lUrl = new JLabel("URL :  ");
		lMethod = new JLabel("M�thode :  ");
		lHeaders = new JLabel("Liste des headers (NOM / VALEUR):  ");
		lParameters = new JLabel("Liste des param�tres (NOM / VALEUR) :  ");
		lResult = new JLabel("R�sultat attendu :  ");
		lNbRequest = new JLabel("Nombre de requetes � ex�cuter :  ");
		lNbConnexion = new JLabel("Nombre de connexions simultan�es :  ");
		
		cb = new ControlButtonForTestWindow(this); 
		
		tfUrl = new JTextField(50);
		tfUrl.setMaximumSize(tfUrl.getPreferredSize());
		cbMethod = new JComboBox(optionsForMethod);
		cbMethod.setMaximumSize(cbMethod.getPreferredSize());
		
		headersPan = new JPanel();
		headersPan.setLayout(new BoxLayout(headersPan, BoxLayout.X_AXIS));
		addedHeadersPan = new JPanel();
		addedHeadersPan.setLayout(new BoxLayout(addedHeadersPan, BoxLayout.Y_AXIS));
		
		parametersPan = new JPanel();
		parametersPan.setLayout(new BoxLayout(parametersPan, BoxLayout.X_AXIS));
		addedParametersPan = new JPanel();
		addedParametersPan.setLayout(new BoxLayout(addedParametersPan, BoxLayout.Y_AXIS));
		
		tfPath = new JTextField(30);
		tfPath.setMaximumSize(tfPath.getPreferredSize());
		tfNbRequests = new JTextField(10);
		tfNbRequests.setMaximumSize(tfNbRequests.getPreferredSize());
		tfNbRequests.setText("1");
		tfNbRequests.addActionListener(cb);
		tfNbConnexions = new JTextField(10);
		tfNbConnexions.setMaximumSize(tfNbConnexions.getPreferredSize());
		tfNbConnexions.setText("1");
		tfNbConnexions.addActionListener(cb);
		tfHeaderName = new JTextField(10);
		tfHeaderName.setMaximumSize(tfHeaderName.getPreferredSize());
		tfHeaderValue = new JTextField(10);
		tfHeaderValue.setMaximumSize(tfHeaderValue.getPreferredSize());
		tfParameterName = new JTextField(10);
		tfParameterName.setMaximumSize(tfParameterName.getPreferredSize());
		tfParameterValue = new JTextField(10);
		tfParameterValue.setMaximumSize(tfParameterName.getPreferredSize());
		
		iiOK = new ImageIcon("images/accept.png");
		iiCancel = new ImageIcon("images/cancel.jpg");
		iiAdd = new ImageIcon("images/add.gif");
		
		bAddHeader = new JButton(iiAdd);
		bAddHeader.setPreferredSize(new Dimension(iiAdd.getIconWidth(), iiAdd.getIconHeight()));
		bAddHeader.addActionListener(cb);
		bAddParameter = new JButton(iiAdd);
		bAddParameter.setPreferredSize(new Dimension(iiAdd.getIconWidth(), iiAdd.getIconHeight()));
		bAddParameter.addActionListener(cb);
		bResult = new JButton("...");
		bResult.addActionListener(cb);
		bAccept = new JButton(iiOK);
		bAccept.addActionListener(cb);
		bCancel = new JButton(iiCancel);
	}
	
	private void makeWindow(){
		getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		
		JPanel mainPan = new JPanel();
		mainPan.setLayout(new BoxLayout(mainPan, BoxLayout.Y_AXIS));
		JScrollPane scroll = new JScrollPane(mainPan);
		this.setContentPane(scroll);
		
		JPanel urlPan = new JPanel();
		urlPan.setLayout(new BoxLayout(urlPan, BoxLayout.X_AXIS));
		urlPan.add(lUrl);
		urlPan.add(tfUrl);
		mainPan.add(urlPan);
		
		JPanel methodPan = new JPanel();
		methodPan.setLayout(new BoxLayout(methodPan, BoxLayout.X_AXIS));
		methodPan.add(lMethod);
		methodPan.add(cbMethod);
		mainPan.add(methodPan);
		
		headersPan.add(lHeaders);
		headersPan.add(tfHeaderName);
		headersPan.add(tfHeaderValue);
		headersPan.add(bAddHeader);
		mainPan.add(headersPan);
		mainPan.add(addedHeadersPan);
		
		parametersPan.add(lParameters);
		parametersPan.add(tfParameterName);
		parametersPan.add(tfParameterValue);
		parametersPan.add(bAddParameter);
		mainPan.add(parametersPan);
		mainPan.add(addedParametersPan);

		mainPan.add(lResult);
		mainPan.add(bResult);
		mainPan.add(lNbRequest);
		mainPan.add(tfNbRequests);
		mainPan.add(lNbConnexion);
		mainPan.add(tfNbConnexions);
		mainPan.add(bAccept);
		mainPan.add(bCancel);
	}
	
	MainWindow getMainWindow(){
		return mw;
	}
	
    public void displayFen(String msg, JFrame f) {
        try{
            JOptionPane.showMessageDialog(f,
                msg,
                "Message",
                JOptionPane.ERROR_MESSAGE);
        }
        catch(HeadlessException y){
            System.out.println(y);
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
    
    public void addToList(String name, String value, JPanel target){
    	JLabel lName, lValue;
    	lName = new JLabel(name+ " : ");
    	lValue = new JLabel(value);
    	JPanel pan = new JPanel();
    	pan.setLayout(new BoxLayout(pan, BoxLayout.X_AXIS));
    	pan.add(lName);
    	pan.add(lValue);
    	target.add(pan);
    	target.updateUI();
    }
}
